package com.woniuxy.yuekeuser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.controller.Form.HotolCarForm;
import com.woniuxy.yuekeuser.controller.Form.HotolCarForm;
import com.woniuxy.yuekeuser.dao.ElasticMapper;
import com.woniuxy.yuekeuser.model.HotelCar;
import com.woniuxy.yuekeuser.model.Hotolcar;
import com.woniuxy.yuekeuser.dao.HotolcarMapper;
import com.woniuxy.yuekeuser.model.HotolcarImage;
import com.woniuxy.yuekeuser.model.HotolcarImage;
import com.woniuxy.yuekeuser.service.IHotolcarImageService;
import com.woniuxy.yuekeuser.service.IHotolcarImageService;
import com.woniuxy.yuekeuser.service.IHotolcarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@Service
@Slf4j
public class HotolcarServiceImpl extends ServiceImpl<HotolcarMapper, Hotolcar> implements IHotolcarService {
    @Autowired
    private HotolcarMapper hotolcarMapper;
    @Autowired
    private IHotolcarImageService iHotolcarImageService;
    @Autowired
    private ElasticMapper elasticMapper;

    @Override
    public void addHotelCar(Hotolcar hotolcar) {

        hotolcarMapper.addHotelCar(hotolcar);


        HotelCar hotelCar = new HotelCar();
        hotelCar.setCampsiteId(hotolcar.getCampsiteId());
        hotelCar.setHotelCarId(hotolcar.getHotolcarId());
        hotelCar.setHotelCarNumber(hotolcar.getHotolcarNumber());
        hotelCar.setHotelCarPeopleNumber(hotolcar.getHotolcarPeopleNumber());
        hotelCar.setHotelCarPrice(hotolcar.getHotolcarPrice());
        hotelCar.setHotelCarState(hotolcar.getHotolcarState());
        hotelCar.setHotelCarWords(hotolcar.getHotolcarWords());
        hotelCar.setHotelLocation(hotolcar.getHotolcarLocation());
        hotelCar.setUserId(hotolcar.getUserId());


        elasticMapper.save(hotelCar);
        log.info("service层中，数据成功加入搜索引擎中！");

    }

    @Override
    public int findHotoCarIdByHotoCarName(String hotoCarName) {
        return hotolcarMapper.findHotoCarIdByHotoCarName(hotoCarName);
    }

    @Override
    public List<Hotolcar> findHotocars(String campsiteName,int index ,int size) {
        return hotolcarMapper.findHotocars(campsiteName,index,size);
    }

    @Override
    public List<HotolcarImage> findcarImageByCarId(int id) {
        return hotolcarMapper.findcarImageByCarId(id);
    }

    @Override
    public int findHotolcarCount(String campsiteName) {
        return hotolcarMapper.findHotolcarCount(campsiteName);
    }

    @Override
    public Hotolcar queryHotolCarById(int hotolcarId) {
        return hotolcarMapper.queryHotolCarById(hotolcarId);
    }

    @Override
    public void addHotolcarHeat(Hotolcar hotolcar) {
        Integer hotolcarHeat = hotolcar.getHotolcarHeat();
        Integer hotolcarId = hotolcar.getHotolcarId();
        hotolcarMapper.addHotolcarHeat(hotolcarHeat,hotolcarId);
    }

    @Override
    public IPage<HotolCarForm> findAllHotolcar(Page<HotolCarForm> hotolcarPage, int hotolPeopleNum,int choose,int campsiteId) {
        IPage<HotolCarForm> allHotolcar = hotolcarMapper.findAllHotolcar(hotolcarPage, hotolPeopleNum,choose,campsiteId);
        List<HotolCarForm> records = allHotolcar.getRecords();
        if (records.size()>0){
            for (int i = 0; i <records.size() ; i++) {
                HotolCarForm hotolCarForm = records.get(i);
                List<HotolcarImage> hotolcarImages = iHotolcarImageService.queryHotolImageByHotolcarId(hotolCarForm.getHotolcarId());
                HotolcarImage hotolcarImage = hotolcarImages.get(0);
                hotolCarForm.setHotolcarImageAddress(hotolcarImage.getHotolcarImageAddress());
            }
        }


        return allHotolcar;
    }
}
